class Obj_Reader(object):
    
    def __init__(self, file_name):
        self._file_name = file_name
        self._vertices = []
        self._textures = []
        self._vertex_normals = []
        self._faces = []

        
    def _get_face(self, line):
        polygon = []
        for point in line:
            vertice = []
            point = point.split("/")
            if len(point)==1:
                vertice.append(self.vertices[int(point[0])-1])
                vertice.append([])
                vertice.append([])
            elif len(point)==3:
                if point[1]=="":
                    vertice.append(self.vertices[int(point[0])-1])
                    vertice.append([])
                    vertice.append(self.vertex_normals[int(point[2])-1])
                else:
                    vertice.append(self.vertices[int(point[0])-1])
                    vertice.append(self.textures[int(point[1])-1])
                    vertice.append(self.vertex_normals[int(point[2])-1])
            else:
                raise Exception("Fehlerhafte Datei!")
            polygon.append(vertice)
        return polygon


        
    def read_file(self):
        f = open(self.file_name, 'r')

        for line in f.readlines():
            line = line.split()
            if len(line) == 0:
                continue
            elif len(line) != 4:
                raise Exception("Fehlerhafte Datei!")
            elif line[0] == "v":
                self.vertices.append([float(line[1]), float(line[2]), float(line[3])])
            elif line[0] == "vt":
                self.textures.append([float(line[1]), float(line[2]), float(line[3])])
            elif line[0] == "vn":
                self.vertex_normals.append([float(line[1]), float(line[2]), float(line[3])])
            elif line[0] == "f":
                self.faces.append(self._get_face(line[1:]))
            else:
                raise Exception("Fehlerhafte Datei!")

    def __repr__(self):
        output = ""
        if len(self.faces)==0:
            output = "Datei wurde noch nicht gelesen mit read_file() oder ist fehlerhaft!"
        else:
            output = "Anzahl der Vertices(Eckpunkte): %s\n" % len(self.vertices)
            output += "Anzahl der Texturen: %s\n" % len(self.textures)
            output += "Anzahl der Vertex-Normalen: %s\n" % len(self.vertex_normals)
            output += "Anzahl der Faces(Polygone): %s" % len(self.faces)
        return output

    # Properties
    def _get_vertices(self):
        return self._vertices
    def _setVertices(self, other):
        self._vertices = other        
    vertices = property(_get_vertices, _setVertices, None)

    def _get_file_name(self):
        return self._file_name
    def _set_file_name(self, other):
        self._file_name = other        
    file_name = property(_get_file_name, _set_file_name, None)

    def _get_textures(self):
        return self._textures
    def _set_textures(self, other):
        self._textures = other        
    textures = property(_get_textures, _set_textures, None)

    def _get_vertex_normals(self):
        return self._vertex_normals
    def _set_vertex_normals(self, other):
        self._vertex_normals = other        
    vertex_normals = property(_get_vertex_normals, _set_vertex_normals, None)

    def _get_faces(self):
        return self._faces
    def _set_faces(self, other):
        self._faces = other        
    faces = property(_get_faces, _set_faces, None)

        
if __name__ == "__main__":
    file_name = "triangle_without_normals.obj"
    obj_reader = Obj_Reader(file_name)
    obj_reader.read_file()
    print obj_reader 
    
    

    

                                
